<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;

class JobsController extends RestBaseController
{
    protected $postModel;

    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    public function getLocations()
    {
        $list = Db::table("zp_location")->column("name");
        return $list;
    }

    public function getCategroy()
    {
        $list = Db::table("zp_category")->select();
        return $list;
    }

    public function getjoblist()
    {
        $page = $this->request->param('page', 1, 'intval');
        $size = $this->request->param('size', 10, 'intval');
        $search = $this->request->param('search', '', 'string');
        $start = ($page - 1) * $size;
        $categry_id = $this->request->param('categry_id', 0, 'intval');
        $job_location = $this->request->param('job_location', '', 'string');
        $where = [];
        if ($categry_id) {
            $where['category_id'] = $categry_id;
        }
        if ($job_location) {
            $where['location'] = $job_location;
        }
        if ($search) {
            $where['name|descrption'] = ['like', "%$search%"];
        }
        $list = Db::table("zp_jobs")
            ->where($where)
            ->limit($start, $size)
            ->order("id desc")
            ->select();
        // echo Db::table("zp_jobs")->getLastSql();die;
        return $list;
    }

    public function getJobDetails()
    {
        $id = $this->request->param('id', 0, 'intval');
        $job = Db::table("zp_jobs")->find($id);
        $company = Db::table("zp_company")->find($job['company_id']);
        return ['job' => $job, 'company' => $company];
    }

    //投递简历
    public function resumeJob()
    {
        $job_id = $this->request->param('job_id', 0, 'intval');
        $user_id = $this->request->param('user_id', 0, 'intval');

        $education = Db::table("zp_education")->where(['uid' => $user_id])->count();
        $workexperience = Db::table("zp_workexperience")->where(['uid' => $user_id])->count();
        if (!$education || $workexperience) {
            return ['status' => false, 'msg' => "请完善简历",'navigate'=>true];
        }
        $resume = Db::table("zp_delivery")->where(['user_id' => $user_id, 'job_id' => $job_id])->count();
        if ($resume == 0) {
            $res = Db::table("zp_delivery")->insert(['user_id' => $user_id, 'job_id' => $job_id, 'create_time' => time()]);
        } else {
            $res = 0;
        }
        if ($res) {
            return ['status' => true, 'msg' => "投递成功"];
        } else {
            return ['status' => false, 'msg' => "您已经投递过这个职位"];
        }
    }

    public function uploadResume()
    {
        $userid = $this->request->param('userid', 0, 'intval');
        if (!empty($_FILES["file"]['name']) && $userid) {
            if ($_FILES["file"]["size"] < 2048000) {
                if ($_FILES["file"]["error"] > 0) {
                    return ['status' => false, 'msg' => '上传失败'];
                } else {
                    $ext = explode(".", $_FILES["file"]["name"]);
                    $ext = $ext[sizeof($ext) - 1];
                    $touxiang = "../upload/resumes/" . MD5(time()) . '.' . $ext;
                    move_uploaded_file($_FILES["file"]["tmp_name"], $touxiang);
                }
            } else {
                echo "Invalid file";
                return ['status' => false, 'msg' => '上传失败'];
            }

            $user = Db::table("zp_user")->find($userid);

            if ($user['resume']) {
                @unlink("../" . $user['resume']);
            }

            $touxiang = \substr($touxiang, 3);
            $data['resume'] = $touxiang;
            Db::table("zp_user")->where(['id' => $userid])->update(['resume' => $touxiang]);
            return ['status' => true, 'msg' => '上传成功'];
        } else {
            return ['status' => false, 'msg' => '上传失败'];
        }
    }

    public function setUserFavor()
    {
        $userid = $this->request->param('user_id', 0, 'intval');
        $jobid = $this->request->param('job_id', 0, 'intval');
        $count = Db::table("zp_favor")->where(['job_id' => $jobid, 'user_id' => $userid])->count();
        if ($count > 0) {
            Db::table("zp_favor")->where(['job_id' => $jobid, 'user_id' => $userid])->delete();
            return ['status' => false, 'msg' => '取消收藏成功'];
        } else {
            Db::table("zp_favor")->data(['job_id' => $jobid, 'user_id' => $userid, 'create_time' => time()])->insert();
            return ['status' => true, 'msg' => '收藏成功'];
        }
    }

    public function getUserFavor()
    {
        $userid = $this->request->param('user_id', 0, 'intval');
        $jobid = $this->request->param('job_id', 0, 'intval');
        $count = Db::table("zp_favor")->where(['job_id' => $jobid, 'user_id' => $userid])->count();
        return ['status' => true, 'count' => $count];
    }
}
